package model

import (
	"gorm.io/gorm"
)

type Shop struct {
	gorm.Model
	ShopName string `gorm:"type:varchar(10);not null;comment: 商品名称"`
	Desc     string `gorm:"type:varchar(50);not null;comment: 商品描述"`
	ShopUrl  string `gorm:"type:varchar(255);not null;comment: 商品图片"`
}

func (s *Shop) CreateShop(db *gorm.DB) error {
	return db.Create(&s).Error
}

func (s *Shop) ShopList(db *gorm.DB, page, pageSize int) (list *Shop, err error, count int64) {

	db.Scopes(Paginate(page, pageSize)).Find(&Shop{})
	db.Model(&s).Find(&s)
	return

}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
